/**
 * 应用状态管理类
 * 负责管理应用的所有状态数据
 */

export class AppState {
    constructor() {
        this.state = {
            chipModel: 'ESP32-WROOM-32',
            projectName: 'esp32_project',
            features: {
                wifi: false,
                bluetooth: false,
                httpServer: false,
                ota: false
            },
            pinConfigurations: {},
            projectFiles: {},
            serialPort: null
        };

        this.listeners = {};
    }

    // 订阅状态变化
    subscribe(listener) {
        const id = Math.random().toString(36).substr(2, 9);
        this.listeners[id] = listener;
        return id;
    }

    // 取消订阅
    unsubscribe(id) {
        delete this.listeners[id];
    }

    // 通知状态变化
    notify(change) {
        Object.values(this.listeners).forEach(listener => {
            listener(change);
        });
    }

    // 获取状态
    getState() {
        return { ...this.state };
    }

    // 设置芯片型号
    setChipModel(model) {
        this.state.chipModel = model;
        this.saveConfig();
        this.notify({ type: 'CHIP_MODEL_CHANGED', payload: model });
    }

    // 设置项目名称
    setProjectName(name) {
        this.state.projectName = name;
        this.saveConfig();
        this.notify({ type: 'PROJECT_NAME_CHANGED', payload: name });
    }

    // 设置功能特性
    setFeatures(features) {
        this.state.features = { ...this.state.features, ...features };
        this.saveConfig();
        this.notify({ type: 'FEATURES_CHANGED', payload: features });
    }

    // 设置引脚配置
    setPinConfig(pin, mode) {
        this.state.pinConfigurations[pin] = { mode };
        this.saveConfig();
        this.notify({ type: 'PIN_CONFIG_CHANGED', payload: { pin, mode } });
    }

    // 设置项目文件
    setProjectFiles(files) {
        this.state.projectFiles = files;
        this.notify({ type: 'PROJECT_FILES_CHANGED', payload: files });
    }

    // 设置串口
    setSerialPort(port) {
        this.state.serialPort = port;
        this.notify({ type: 'SERIAL_PORT_CHANGED', payload: port });
    }

    // 获取芯片型号
    getChipModel() {
        return this.state.chipModel;
    }

    // 获取项目名称
    getProjectName() {
        return this.state.projectName;
    }

    // 获取功能特性
    getFeatures() {
        return { ...this.state.features };
    }

    // 获取引脚配置
    getPinConfigurations() {
        return { ...this.state.pinConfigurations };
    }

    // 获取项目文件
    getProjectFiles() {
        return { ...this.state.projectFiles };
    }

    // 获取串口
    getSerialPort() {
        return this.state.serialPort;
    }

    // 保存配置到localStorage
    saveConfig() {
        const config = {
            chipModel: this.state.chipModel,
            projectName: this.state.projectName,
            features: this.state.features,
            pinConfigurations: this.state.pinConfigurations
        };
        localStorage.setItem('esp32_config', JSON.stringify(config));
    }

    // 加载配置
    loadConfig(config) {
        this.state.chipModel = config.chipModel || this.state.chipModel;
        this.state.projectName = config.projectName || this.state.projectName;
        this.state.features = { ...this.state.features, ...config.features };
        this.state.pinConfigurations = config.pinConfigurations || this.state.pinConfigurations;
    }
}